<?php

namespace app\common\model;

use think\Model;
class ChatGroupContent extends Model
{
    // 定义时间戳字段名
    protected $createTime = 'add_time';
    // 追加属性
    protected $append = ['user_info'];
    protected $resultSetType = 'collection';
    public static function get_content_list($msg_id,$room_id,$group_user_id,$group_type,$is_jin)
    {
        $where = " FIND_IN_SET($room_id,room_ids)";
        if($msg_id){
            $where.= " AND id < $msg_id";
        }
        $order = "id DESC";
        $field = "id,user_id,room_ids,content,cover,type,add_time";
        $list = self::where($where)->limit(20)->field($field)->order($order)->select()->toArray();
        foreach ($list as &$value){
            if($value['type'] == 2){
                $value['content'] = splicing_domain($value['content']);
            }
            if($value['type'] == 3){
                $value['content'] = splicing_domain($value['content']);
                $value['cover'] = splicing_domain($value['cover']);
            }
        }
        $data = [
            'group_user_id'=>$group_user_id,
            'group_type'=>$group_type,
            'is_jin'=>$is_jin,
            'is_group'=>0,
            'is_bid'=>0,
            'list'=>array_reverse($list),
        ];
        return $data;
    }

    public static function get_content_group($msg_id,$room_id,$group_user_id,$group_type,$is_jin,$user_id,$group_room)
    {
        $where = " FIND_IN_SET($room_id,room_ids)";
        if($msg_id){
            $where.= " AND id < $msg_id";
        }
        $order = "id DESC";
        $field = "id,user_id,group_id,room_ids,content,cover,type,add_time";
        $list = self::where($where)->limit(20)->field($field)->order($order)->select()->toArray();
        if($group_type == 2){
            $is_group = InquiryForm::where(["group_id"=>$group_room])->find();
            $inquiry_form = InquiryForm::where(["group_id"=>$group_room,"is_bid"=>'2'])->find();
            if($is_group){
                $is_group = 1; //已发布询价
            }else{
                $is_group = 0;
            }
            if($inquiry_form){
                $is_bid = 1; //已确认成交
            }else{
                $is_bid = 0;
            }
        }else{
            $is_group = 0;
            $is_bid = 0;
        }
        foreach ($list as &$value){
            if($value['type'] == 2){
                $value['content'] = splicing_domain($value['content']);
            }
            if($value['type'] == 3){
                $value['content'] = splicing_domain($value['content']);
                $value['cover'] = splicing_domain($value['cover']);
            }
        }
        $data = [
            'group_user_id'=>$group_user_id,
            'group_type'=>$group_type,
            'is_jin'=>$is_jin,
            'is_group'=>$is_group,
            'is_bid'=>$is_bid,
            'list'=>array_reverse($list),
        ];
        return $data;
    }

    public static function get_content_info($msg_id,$room_id)
    {
        $where = " FIND_IN_SET($room_id,room_ids)";
        if($msg_id){
            $where.= " AND id = $msg_id";
        }
        $field = "*";
        $info = self::where($where)->field($field)->find();
        if($info && !empty($info['type'])){
            if($info['type'] == 2 || $info['type'] == 3){
                $info['content'] = splicing_domain($info["content"]);
            }
        }
        return $info;
    }

    public function getUserInfoAttr($value,$data)
    {
        $info = User::where("user_id",$data["user_id"])->field("username,image,authen,authen_time,deposit_price")->find();
        $user_info['username'] = $info["username"];
        $user_info['user_image'] = $info["image"];
        return $user_info;
    }

    public function getAddTimeAttr($value)
    {
        if($value){
            return getChatTimeStr($value);
        }else{
            return "";
        }
    }
}
